package com.movie.controller;

import com.movie.model.Movie;
import com.movie.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/movie")
public class MovieController {

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @Autowired
    private MovieService movieService;

    @GetMapping("/list")
    @ResponseBody
    public List<Movie> getAllMovies() {
        return movieService.getAllMovies();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Movie getMovie(@PathVariable Integer id) {
        return movieService.getMovieById(id);
    }

    @PostMapping("/add")
    @ResponseBody
    public String addMovie(@RequestBody Movie movie) {
        movieService.addMovie(movie);
        return "success";
    }

    @PostMapping("/update")
    @ResponseBody
    public String updateMovie(@RequestBody Movie movie) {
        movieService.updateMovie(movie);
        return "success";
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public String deleteMovie(@PathVariable Integer id) {
        movieService.deleteMovie(id);
        return "success";
    }
}